@using Microsoft.AspNetCore.Identity
@using OrchardCore.Entities
@using OrchardCore.Settings
@using OrchardCore.Users
@using OrchardCore.Users.Models
@model OrchardCore.Users.ViewModels.LoginViewModel
@inject SignInManager<IUser> SignInManager
@inject ISiteService SiteService
@inject UserManager<IUser> UserManager;
@{
    ViewLayout = "Layout__Login";
    // ViewLayout = "Layout__MyLogin";
    var userCanRegister = (await SiteService.GetSiteSettingsAsync()).As<RegistrationSettings>().UsersCanRegister == UserRegistrationType.AllowRegistration;
    var allowResetPassword = (await SiteService.GetSiteSettingsAsync()).As<ResetPasswordSettings>().AllowResetPassword;
    var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList();
    var disableLocalLogin = (await SiteService.GetSiteSettingsAsync()).As<LoginSettings>().DisableLocalLogin;
}

<style asp-name="font-awesome" version="6"></style>

<div class="row">
    @if (!disableLocalLogin)
    {
        <div class="col-md-6 @(loginProviders.Count == 0 ? "offset-md-3" : String.Empty)">
            <form asp-controller="Account" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="auth-form no-multisubmit">
                <h1 class="fs-4">@T["Log in"]111111111</h1>
                <hr />
                <div asp-validation-summary="All"></div>
                <div class="mb-3">
                    <label asp-for="UserName">@T["Username or email address"]</label>
                    <input asp-for="UserName" class="form-control" autofocus tabindex="1" />
                    <span asp-validation-for="UserName" class="text-danger"></span>
                </div>
                <div class="mb-3">
                    <label asp-for="Password">
                        @T["Password"]
                        @if (allowResetPassword)
                        {
                            <a asp-controller="ResetPassword" asp-action="ForgotPassword" class="label-link">@T["Forgot your password?"]</a>
                        }
                    </label>
                    <div class="input-group">
                        <input asp-for="Password" class="form-control" tabindex="2" />
                        <button tabindex="-1" class="btn btn-secondary" type="button" id="togglePassword"><i class="icon fa fa-eye"></i></button>
                    </div>
                    <span asp-validation-for="Password" class="text-danger"></span>
                </div>
                <div class="mb-3 form-check">
                    <input asp-for="RememberMe" class="form-check-input" tabindex="3">
                    <label asp-for="RememberMe" class="form-check-label">@T["Remember me"]</label>
                </div>
                @await RenderSectionAsync("AfterLogin", required: false)
                <div></div>
                <button type="submit" class="btn btn-primary" tabindex="4">@T["Log in"]</button>
                @if (userCanRegister)
                {
                    <div class="mt-4">
                        <a asp-controller="Registration" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]">@T["Register as a new user"]</a>
                    </div>
                }
            </form>
        </div>
    }
    @if (loginProviders.Count > 0)
    {
        <div class="col-md-6 @(disableLocalLogin ? "offset-md-3" : String.Empty)">
            <section>
                <h4>@T["Use another service to log in"]</h4>
                <hr />
                <form asp-controller="Account" asp-action="ExternalLogin" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal no-multisubmit">
                    <div>
                        <p>
                            @foreach (var provider in loginProviders)
                            {
                                <button type="submit" class="btn btn-primary @(disableLocalLogin ? "w-100" : String.Empty)" name="provider" value="@provider.Name" title="@T["Log in using your {0} account", provider.DisplayName]">@provider.DisplayName</button>
                                @if (disableLocalLogin)
                                {
                                    <br />
                                }
                            }
                        </p>
                    </div>
                </form>
            </section>
        </div>
    }
</div>
<script at="Foot">
    togglePassword = document.querySelector('#togglePassword');
    password = document.querySelector('#Password');

    togglePassword.addEventListener('click', function (e) {
        @* toggle the type attribute *@
        type = password.getAttribute('type') === 'password' ? 'text' : 'password';
        password.setAttribute('type', type);

        @* toggle the eye slash icon *@
        icon = this.getElementsByClassName('icon')[0];
        if (icon.getAttribute('data-icon')){ @* if the icon is rendered as a svg *@
            type === 'password' ? icon.setAttribute('data-icon', 'eye') : icon.setAttribute('data-icon', 'eye-slash');
        }
        else{ @* if the icon is still a <i> element *@
            type === 'password' ? icon.classList.remove('fa-eye-slash') : icon.classList.remove('fa-eye');
            type === 'password' ? icon.classList.add('fa-eye') : icon.classList.add('fa-eye-slash');
        }
    });
</script>
